package com.in.ussd;

import java.io.File;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.in.ussd.R;
import com.in.ussd.constant.ConstantVariables;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class BaseActivity extends Activity implements OnClickListener {

	public SharedPreferences mPrefs;
	SharedPreferences.Editor editor;
	AdView adView;
	String AD_UNIT_ID = "a152aebb9343d4e";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.layout_header);

		mPrefs = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());
		int old_versioncode = mPrefs.getInt(ConstantVariables.VERSION_CODE, 0);
		int v = 0;
		try {
			v = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
			if (v != old_versioncode) {

				// Delete old database file
				File dbFile = getDatabasePath(ConstantVariables.DATABASE_NAME);
				dbFile.deleteOnExit();

				editor = mPrefs.edit();
				editor.putInt(ConstantVariables.VERSION_CODE, v);
				editor.commit();
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.sharebtn:
			Intent intentShare = new Intent(Intent.ACTION_SEND);
			intentShare.setType("text/plain");
			intentShare
					.putExtra(
							android.content.Intent.EXTRA_TEXT,
							getString(R.string.app_name)
									+ " /r/n "
									+ "https://play.google.com/store/apps/details?id=com.in.ussd");
			startActivity(Intent.createChooser(intentShare, "Share"));
			break;

		default:
			break;
		}
	}

	protected void setupAdmob() {
		// Create an ad.
		adView = new AdView(this);
		adView.setAdSize(AdSize.BANNER);
		adView.setAdUnitId(AD_UNIT_ID);
		LinearLayout layout = (LinearLayout) findViewById(R.id.rootLayout);
		layout.addView(adView);

		final TelephonyManager tm = (TelephonyManager) getBaseContext()
				.getSystemService(Context.TELEPHONY_SERVICE);
		String deviceid = tm.getDeviceId();
		AdRequest adRequest = new AdRequest.Builder()
				.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
				.addTestDevice(deviceid).build();

		// AdRequest adRequest = new AdRequest.Builder().build();
		adView.loadAd(adRequest);
	}

	protected void share() {

		ImageView shareImg = (ImageView) findViewById(R.id.sharebtn);
		shareImg.setOnClickListener(this);
	}

	/** Called before the activity is destroyed. */
	@Override
	public void onDestroy() {
		// Destroy the AdView.
		if (adView != null) {
			adView.destroy();
		}
		super.onDestroy();
	}
}
